﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp1.DB;
using ConsoleApp1.Model;
using MySqlSugar;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.PhantomJS;

namespace ConsoleApp1
{
   public  class AvNo1Spider
    {
        public static void run(int startpage, int endpage)
        {
            IWebDriver MainPage = new ChromeDriver();
            var DetailPage = new ChromeDriver();
            try
            {
                for (int i = startpage; i <= endpage; i++)
                {


                    var ts = new TimeSpan(0, 15, 0);

                    MainPage.Manage().Window.Maximize();
                    MainPage.Manage().Timeouts().SetPageLoadTimeout(ts);
                    MainPage.Manage().Timeouts().ImplicitlyWait(ts);
                    MainPage.Manage().Timeouts().SetScriptTimeout(ts);

                    DetailPage.Manage().Window.Maximize();
                    DetailPage.Manage().Timeouts().SetPageLoadTimeout(ts);
                    DetailPage.Manage().Timeouts().ImplicitlyWait(ts);
                    DetailPage.Manage().Timeouts().SetScriptTimeout(ts);
                    INavigation n = MainPage.Navigate();
                    MainPage.Url = "http://www.playno1.com";
                    DetailPage.Url = "http://www.playno1.com";
                   
                    
                        MainPage.FindElement(By.XPath(@".//*[@class='ui-dialog-buttonset']/button[1]")).Click();
                        DetailPage.FindElement(By.XPath(@".//*[@class='ui-dialog-buttonset']/button[1]")).Click();
                 
                  

                    n.GoToUrl("http://www.playno1.com/portal.php?mod=list&catid=78&page=" + i);
                    n.Refresh();
                    var titles = MainPage.FindElements(By.XPath(@".//*[@class='fire_float']/ul/li"));
                    var avinfos = new List<AvInfo>();
                    foreach (var item in titles){



                        var newsurl = item.FindElement(By.TagName("a")).GetAttribute("href");
                        var aRepository = new BaseRepository<AvInfo>();
                        if (aRepository.Find(x => x.contentremoteurl == newsurl).ToList().Count <= 0)
                        {
                            AvInfo avinfo = new AvInfo();
                            var title = item.FindElement(By.TagName("h3")).Text;
                            Console.WriteLine(title);
                            var imgsrc = item.FindElement(By.TagName("img")).GetAttribute("src");
                            var imgfile = ImageHelper.SaveImage(imgsrc);
                            Console.WriteLine(imgfile);

                            Console.WriteLine(newsurl);
                            DetailPage.Url = newsurl;

                            var images = DetailPage.FindElements(By.XPath(@".//img"));
                            var pagesource = DetailPage.PageSource.ToLower();

                            pagesource = pagesource.Replace(@"http://www.playno1.com/", string.Empty);
                            var removead = pagesource.IndexOf(@"<div class=""h hm"">");
                            pagesource = pagesource.Remove(0, removead);
                            Console.WriteLine(images.Count);
                            foreach (var VARIABLE in images)
                            {
                                var imagefile = VARIABLE.GetAttribute("src");

                                var localimagefile = ImageHelper.SaveImage(imagefile);
                                localimagefile = "../image/" + Path.GetFileName(localimagefile);
                                Console.WriteLine("oldimage=  " + imagefile);
                                Console.WriteLine("localimage=  " + localimagefile);
                                pagesource = pagesource.Replace(imagefile.ToLower(), localimagefile.ToLower());

                            }
                            var content = DetailPage.FindElement(By.Id("article_content")).Text;

                            //var contentscreenshot = ImageHelper.SaveScreenShot(DetailPage, newsurl);
                            var contenturl = HtmlHelper.SaveHtml(pagesource,title);

                            Console.WriteLine(content);
                            var publishtime = item.FindElement(By.ClassName("fire_dv")).FindElement(By.ClassName("fire_left")).Text;
                            Console.WriteLine(publishtime);
                            avinfo.title = title;
                            avinfo.imageremoteurl = imgsrc;
                            avinfo.imagelocalurl = imgfile;
                            avinfo.contentremoteurl = newsurl;
                            avinfo.introduce = content;
                            avinfo.publishtime = DateTime.Parse(publishtime);
                            avinfo.contentlocalurl = contenturl;
                            avinfo.content = pagesource;
                            aRepository.Add(avinfo);
                            /*
                            
                            var title = item.FindElement(By.TagName("h3")).Text;
                            Console.WriteLine(title);
                            var imgsrc = item.FindElement(By.TagName("img")).GetAttribute("src");
                            var imgfile = ImageHelper.SaveImage(imgsrc);
                            Console.WriteLine(imgfile);
                         
                            Console.WriteLine(newsurl);
                            DetailPage.Url = newsurl;

                            //var images = DetailPage.FindElementById("article_content").FindElements(By.TagName("img"));

                       
                            var pagesource = DetailPage.PageSource.ToLower();
                            pagesource = pagesource.Replace(@"http://www.playno1.com/", string.Empty);
                            var removead = pagesource.IndexOf(@"<div class=""h hm"">");
                            pagesource = pagesource.Remove(0, removead);
                            var images = DetailPage.FindElements(By.XPath(".//img"));
                            foreach (var img in images)
                            {
                                var todownimageurl = img.GetAttribute("scr");
                                Console.WriteLine("downloading image....:" + todownimageurl);
                                var localimagefile = ImageHelper.SaveImage(todownimageurl);

                                localimagefile = "../image/" + Path.GetFileName(localimagefile);
                                pagesource = pagesource.Replace(todownimageurl, localimagefile.ToLower());

                            }
                            pagesource = HtmlHelper.ReplaceHtml(pagesource);
                            var contenturl = HtmlHelper.SaveHtml(pagesource, title);
                            var content = DetailPage.FindElement(By.Id("article_content")).Text;
                          
                            Console.WriteLine(content);
                            var publishtime =
                                item.FindElement(By.ClassName("fire_dv")).FindElement(By.ClassName("fire_left")).Text;
                           

                            Console.WriteLine(publishtime);
                            //var contentscreenshot = ImageHelper.SaveScreenShot(DetailPage, newsurl);
                         
                            */

                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("错误:"+e.ToString());
                MainPage.Close();
                DetailPage.Close();
                MainPage.Quit();
                DetailPage.Quit();
                
                run(startpage,endpage);
            }
            finally
            {
                MainPage.Quit();
                DetailPage.Quit();
            }
         
            
        }
    }
}
